package com.example.guava;

import com.google.common.util.concurrent.SimpleTimeLimiter;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class TimeLimiterMain {

    public static void main(String[] args) {
        ExecutorService executorService = Executors.newCachedThreadPool();
        SimpleTimeLimiter simpleTimeLimiter = SimpleTimeLimiter.create(executorService);
        try {
            simpleTimeLimiter.runWithTimeout(new Thread(()->{
                System.out.println("1");
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }),1l, TimeUnit.SECONDS);
        } catch (TimeoutException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}
